using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.ComponentModel;
using System.Collections;

/// <summary>
/// Represents an application in ResourceBlender
/// </summary>
public class RBApplication : IComparable {

    /// <summary>
    /// Gets or sets the ID.
    /// </summary>
    /// <value>The ID.</value>
    public string ID { get;set;}
    
    /// <summary>
    /// Gets or sets the name of the application.
    /// </summary>
    /// <value>The name of the application.</value>
    public string ApplicationName { get; set; }

    /// <summary>
    /// Gets or sets the display name.
    /// </summary>
    /// <value>The display name.</value>
    public string DisplayName { get; set; }

    /// <summary>
    /// Gets or sets the default language.
    /// </summary>
    /// <value>The default language.</value>
    public string DefaultLanguage { get; set; }

    /// <summary>
    /// Gets or sets the language codes used by the application.
    /// </summary>
    /// <value>The languages.</value>
    public List<string> Languages { get; set; }

    /// <summary>
    /// Initializes a new <see cref="RBApplication"/>.
    /// </summary>
    public RBApplication() {
        this.ID = string.Empty;
        this.ApplicationName = string.Empty;
        this.DisplayName = string.Empty;
        this.DefaultLanguage = string.Empty;
        this.Languages = new List<string>();
    }

    /// <summary>
    /// Initializes a new <see cref="RBApplication"/>.
    /// </summary>
    /// <param name="reader">The reader.</param>
    public RBApplication(DbDataReader reader) {
        if(reader.FieldCount < 5) {
            throw new ArgumentException("Reader must contain all fields");
        }
        this.ID = reader["id"].ToString();
        this.ApplicationName = reader["name"].ToString();
        this.DisplayName = reader["displayname"].ToString();
        this.DefaultLanguage = reader["defaultlanguage"].ToString();
        this.Languages = new List<string>(reader["languages"].ToString().Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries));
    }

    /// <summary>
    /// Creates a deep copy of this instance.
    /// </summary>
    /// <returns></returns>
    public RBApplication Clone() {
        RBApplication clone = new RBApplication();
        clone.ID = this.ID;
        clone.ApplicationName = this.ApplicationName;
        clone.DisplayName = this.DisplayName;
        clone.DefaultLanguage = this.DefaultLanguage;
        clone.Languages = this.Languages;
        return clone;
    }

    /// <summary>
    /// Compares this RBApplication to another one by name.
    /// </summary>
    /// <param name="obj">An object to compare with this instance.</param>
    /// <returns>
    /// </returns>
    /// <exception cref="T:System.ArgumentException">
    /// 	<paramref name="obj"/> is not the same type as this instance.
    /// </exception>
    public int CompareTo(object obj) {
        if(obj == null || !(obj is RBApplication)) {
            return 1;
        } else {
            return this.DisplayName.CompareTo((obj as RBApplication).DisplayName);
        }
    }
}
